package com.walker.component.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则工具类
 * @author james.deng
 *
 */
public class PatternTools {
	
	public static String getGroupOne(String obj,String pattern){
		return getGroup(obj, pattern, 1);
	}
	public static String getGroup(String obj,String pattern,int index){
		Pattern grepPattern = Pattern.compile(pattern);
		if (grepPattern != null) {
			Matcher m = grepPattern.matcher(obj.toString());
			if (m.find()) {
				return m.group(index);
			} else {
				return "";
			}
		} else {
			return obj;
		}
	}
	
	public static List<String> getGroup(String obj,String pattern){
		Pattern grepPattern = Pattern.compile(pattern);
		List<String> list = null;
		if (grepPattern != null) {
			Matcher m = grepPattern.matcher(obj.toString());
			while(m.find()){
				if(list == null) list = new ArrayList<String>();
				list.add(m.group()); 
		    }
		}
		return list;
	}
}
